package com.springmvc.aop;

import com.example.springmvc.controller.Usercontroller;
import com.example.springmvc.mapper.Usermapper;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Aspect
public class MyAdvice {

    @Autowired
    Usercontroller usercontroller;
    @Pointcut("execution(* com.example.springmvc.controller.Usercontroller.login(..))")
    private void pt(){

    }
    @Resource
    Usermapper usermapper;
    @After("pt()")
    public void insertlog() throws UnknownHostException {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String logtime = formatter.format(date);
        InetAddress addr = InetAddress.getLocalHost();
        usermapper.Insertlog(addr.getHostAddress(),usercontroller.getUserName(),logtime);
    }
}
